package ru.ccfit.nsu.g7204.dronov.adPlatform;

import ru.ccfit.nsu.g7204.dronov.adPlatform.system.db.DBConnectionHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Drongo
 * Date: 16.03.11
 * Time: 21:19
 * To change this template use File | Settings | File Templates.
 */
public class FeaturesDBManager implements FeatureListSupporter{
    private PreparedStatement addFeatureStm;
    private PreparedStatement delFeatureStm;
    private PreparedStatement editFeatureStm;
    private PreparedStatement getListStm;
    private PreparedStatement delAllStm;

    public FeaturesDBManager(DBConnectionHolder dbConnectionHolder) {
        Connection conn = dbConnectionHolder.getConnection();
        try {
            addFeatureStm=conn.prepareStatement("call addFeature(?)");
            delFeatureStm=conn.prepareStatement("call delFeature(?)");
            editFeatureStm=conn.prepareStatement("update features set info = ? where id = ?");
            getListStm=conn.prepareStatement("select id, info from features");
            delAllStm=conn.prepareStatement("delete from features");
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public void editFeature(int id, String newInfo)
    {
        try {
            editFeatureStm.setString(1,newInfo);
            editFeatureStm.setInt(2,id);
            editFeatureStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public void addFeature(String info)
    {
        try {
            addFeatureStm.setString(1,info);
            addFeatureStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public void delFeature(int id)
    {
        try {
            delFeatureStm.setInt(1,id);
            delFeatureStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public List<Feature> getFeatureList()
    {
        List<Feature> result=null;
        try {
            ResultSet rs=getListStm.executeQuery();
            result = new ArrayList<Feature>();
            for (;rs.next();)
            {
                result.add(new Feature(rs.getInt("id"),rs.getString("info")));
            }
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
        return result;
    }

    public void delAll() {
        try {
            delAllStm.executeUpdate();
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }    }
}
